package org.webdb.core;

import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.CharBuffer;

public class IOUtils {

    public static String toString(InputStream in) throws IOException {
        Reader reader = new InputStreamReader(in);
        CharBuffer cbuf = CharBuffer.allocate(1024);
        StringBuilder sb = new StringBuilder();
        while (reader.read(cbuf) != -1) {
            sb.append(cbuf.array());
            cbuf.clear();
        }

        close(in);
        return sb.toString();

    }

    private static void close(Closeable in) {
        try {
            in.close();
        } catch (Exception e) {
        }
    }
}
